package com.ruoyi.web.controller.teaching;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.teaching.domain.TeachingClasses;
import com.ruoyi.teaching.domain.TeachingClassesScheduleTemplate;
import com.ruoyi.teaching.domain.TeachingClassesStudents;
import com.ruoyi.teaching.service.ITeachingClassesScheduleTemplateService;
import com.ruoyi.teaching.service.ITeachingClassesService;
import com.ruoyi.teaching.service.ITeachingClassesStudentsService;
import org.apache.shiro.authz.annotation.RequiresGuest;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.teaching.domain.TeachingClassesSchedule;
import com.ruoyi.teaching.service.ITeachingClassesScheduleService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 排课信息Controller
 *
 * @author William
 * @date 2022-10-17
 */
@Controller
@RequestMapping("/teaching/schedule")
public class TeachingClassesScheduleController extends BaseController {
    private String prefix = "teaching/schedule";

    @Autowired
    private ISysUserService userService;
    @Autowired
    private ITeachingClassesScheduleService teachingClassesScheduleService;
    @Autowired
    private ITeachingClassesService teachingClassesService;
    @Autowired
    private ITeachingClassesStudentsService teachingClassesStudentsService;

    @Autowired
    private ITeachingClassesScheduleTemplateService teachingClassesScheduleTemplateService;

    @RequiresPermissions("teaching:schedule:view")
    @GetMapping()
    public String schedule() {
        return prefix + "/schedule";
    }

    /**
     * 查询排课信息列表
     */
//    @RequiresPermissions("teaching:schedule:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TeachingClassesSchedule teachingClassesSchedule) {
        startPage();
        teachingClassesSchedule.setSchoolId(getSysUser().getSchoolId());
        List<TeachingClassesSchedule> list = teachingClassesScheduleService.selectTeachingClassesScheduleList(teachingClassesSchedule);
        return getDataTable(list);
    }

    /**
     * 导出排课信息列表
     */
    @RequiresPermissions("teaching:schedule:export")
    @Log(title = "排课信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TeachingClassesSchedule teachingClassesSchedule) {
        List<TeachingClassesSchedule> list = teachingClassesScheduleService.selectTeachingClassesScheduleList(teachingClassesSchedule);
        ExcelUtil<TeachingClassesSchedule> util = new ExcelUtil<TeachingClassesSchedule>(TeachingClassesSchedule.class);
        return util.exportExcel(list, "排课信息数据");
    }

    /**
     * 新增排课信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        //找出当前账号所在学校的所有老师
        SysUser param = new SysUser();
        param.setSchoolId(getSysUser().getSchoolId());
        param.setUserType("00");
        List<SysUser> teachers = userService.selectUserList(param);
        mmap.put("teachers", teachers);
        return prefix + "/add";
    }

    /**
     * 新增重复排课
     */
    @GetMapping("/addRepeat")
    public String addRepeat(ModelMap mmap) {
        //找出当前账号所在学校的所有老师
        SysUser param = new SysUser();
        param.setSchoolId(getSysUser().getSchoolId());
        param.setUserType("00");
        List<SysUser> teachers = userService.selectUserList(param);
        mmap.put("teachers", teachers);
        return prefix + "/addRepeat";
    }


    /**
     * 新增保存排课信息
     */
    @RequiresPermissions("teaching:schedule:add")
    @Log(title = "排课信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TeachingClassesSchedule teachingClassesSchedule) {
        Long classId = teachingClassesSchedule.getClassId();
        TeachingClasses classes = teachingClassesService.selectTeachingClassesByClassId(classId);
        if (StringUtils.isNull(classes)) {
            return error("班级不存在");
        }

        TeachingClassesStudents query = new TeachingClassesStudents();
        query.setClassId(classId);
        List<TeachingClassesStudents> list = teachingClassesStudentsService.selectTeachingClassesStudentsList(query);
        //不满4人不让排课
        int limitCount = 4;
        if (list.size() < limitCount) {
            return error("该班级不满4人无法排课");
        }
        teachingClassesSchedule.setSchoolId(classes.getSchoolId());
        teachingClassesSchedule.setCourseId(classes.getCourseId());
        teachingClassesSchedule.setCreateId(getUserId());
        teachingClassesSchedule.setCreateBy(getLoginName());
        teachingClassesSchedule.setCreateTime(new Date());
        //判断相同时间不能重复添加
        return toAjax(teachingClassesScheduleService.insertTeachingClassesSchedule(teachingClassesSchedule));
    }

    /**
     * 修改排课信息
     */
    @RequiresPermissions("teaching:schedule:edit")
    @GetMapping("/edit/{scheduleId}")
    public String edit(@PathVariable("scheduleId") Long scheduleId, ModelMap mmap) {
        TeachingClassesSchedule teachingClassesSchedule = teachingClassesScheduleService.selectTeachingClassesScheduleByScheduleId(scheduleId);
        mmap.put("teachingClassesSchedule", teachingClassesSchedule);
        //找出当前账号所在学校的所有老师
        SysUser param = new SysUser();
        param.setSchoolId(getSysUser().getSchoolId());
        param.setUserType("00");
        List<SysUser> teachers = userService.selectUserList(param);
        mmap.put("teachers", teachers);
        return prefix + "/edit";
    }

    /**
     * 修改排课信息
     */
    @GetMapping("/editRepeat/{templateId}")
    public String editRepeat(@PathVariable("templateId") Long templateId, ModelMap mmap) {
        TeachingClassesScheduleTemplate template = teachingClassesScheduleTemplateService.selectTeachingClassesScheduleTemplateByTemplateId(templateId);
        mmap.put("template", template);
        //找出当前账号所在学校的所有老师
        SysUser param = new SysUser();
        param.setSchoolId(getSysUser().getSchoolId());
        param.setUserType("00");
        List<SysUser> teachers = userService.selectUserList(param);
        mmap.put("teachers", teachers);
        return prefix + "/editRepeat";
    }

    /**
     * 修改保存排课信息
     */
    @RequiresPermissions("teaching:schedule:edit")
    @Log(title = "排课信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TeachingClassesSchedule teachingClassesSchedule) {
        return toAjax(teachingClassesScheduleService.updateTeachingClassesSchedule(teachingClassesSchedule));
    }

    /**
     * 删除排课信息
     */
    @RequiresPermissions("teaching:schedule:remove")
    @Log(title = "排课信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(teachingClassesScheduleService.deleteTeachingClassesScheduleByScheduleIds(ids));
    }
}
